/*******************************************************************************
 * Copyright 2011 alipay.com
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *   http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 ******************************************************************************/

package com.alipay.platform.data.impl.orm;


import com.alipay.platform.core.AlipayApp;

import android.content.ContentProvider;
import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteQueryBuilder;
import android.net.Uri;
import android.util.Log;


/**
 * 数据provider
 * @author sanping.li@alipay.com
 *
 */
public class DataProvider extends ContentProvider {
	public static final String TAG = "DataProvider";
		
	private DatabaseMgr mDatabaseMgr;
	private SQLiteDatabase mDatabase;
	private OrmHelper mOrmHelper;
	
	/* (non-Javadoc)
	 * @see android.content.ContentProvider#delete(android.net.Uri, java.lang.String, java.lang.String[])
	 */
	@Override
	public int delete(Uri uri, String selection, String[] selectionArgs) {
		// TODO Auto-generated method stub
		if(mOrmHelper.getDataMap().containsKey(mOrmHelper.getUriMatcher().match(uri)))
			mDatabase.delete(mOrmHelper.getDataMap().get(mOrmHelper.getUriMatcher().match(uri)), selection, selectionArgs);
		return 0;
	}

	/* (non-Javadoc)
	 * @see android.content.ContentProvider#getType(android.net.Uri)
	 */
	@Override
	public String getType(Uri uri) {
		// TODO Auto-generated method stub
		return null;
	}

	/* (non-Javadoc)
	 * @see android.content.ContentProvider#insert(android.net.Uri, android.content.ContentValues)
	 */
	@Override
	public Uri insert(Uri uri, ContentValues values) {
		long id = -1;
		if(mOrmHelper.getDataMap().containsKey(mOrmHelper.getUriMatcher().match(uri)))
			id = mDatabase.insert(mOrmHelper.getDataMap().get(mOrmHelper.getUriMatcher().match(uri)), null, values);
		Log.i(TAG, "insert"+id+mDatabase.getPath());
		return Uri.parse("" + id);
	}

	/* (non-Javadoc)
	 * @see android.content.ContentProvider#onCreate()
	 */
	@Override
	public boolean onCreate() {
		mOrmHelper = new OrmHelper(getContext());
		AlipayApp alipayApp = (AlipayApp) getContext().getApplicationContext();
		alipayApp.setOrmHelper(mOrmHelper);
		mDatabaseMgr = new DatabaseMgr(mOrmHelper, getContext());
		mDatabase = mDatabaseMgr.getWritableDatabase();
		return true;
	}

	/* (non-Javadoc)
	 * @see android.content.ContentProvider#query(android.net.Uri, java.lang.String[], java.lang.String, java.lang.String[], java.lang.String)
	 */
	@Override
	public Cursor query(Uri uri, String[] projection, String selection,
			String[] selectionArgs, String sortOrder) {
        // TODO Auto-generated method stub
        if(mOrmHelper.getDataMap().containsKey(mOrmHelper.getUriMatcher().match(uri))){
            SQLiteQueryBuilder sqlBuilder = new SQLiteQueryBuilder();
            sqlBuilder.setTables(mOrmHelper.getDataMap().get(mOrmHelper.getUriMatcher().match(uri)));
            return sqlBuilder.query(mDatabase, projection, selection, selectionArgs, null, null, sortOrder);
        }
        return null;
    }

	/* (non-Javadoc)
	 * @see android.content.ContentProvider#update(android.net.Uri, android.content.ContentValues, java.lang.String, java.lang.String[])
	 */
	@Override
	public int update(Uri uri, ContentValues values, String selection,
			String[] selectionArgs) {
		// TODO Auto-generated method stub
		if(mOrmHelper.getDataMap().containsKey(mOrmHelper.getUriMatcher().match(uri)))
			return mDatabase.update(mOrmHelper.getDataMap().get(mOrmHelper.getUriMatcher().match(uri)), values, selection, selectionArgs);
		return 0;
	}

}
